package com.upc.toss.util;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.huaweicloud.sdk.iotda.v5.model.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;


public class Get {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");

    public static String temperature;
    public static String humidity;
    public static String luminance;
    public static String lightStatus;
    public static String motorStatus;
    public static String settingTemperature;
    public static String settingHumidity;
    public static String settingLuminance;
    public static String ifLightOn;
    public static String ifMotorOn;


    public static void getProperties(){

        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(Access.AK)
                .withSk(Access.SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();

        // 实例化请求对象
        ShowDeviceShadowRequest request=new ShowDeviceShadowRequest();
        request.setDeviceId(Access.DEVICE_ID);
        try {
            // 调用查询设备列表接口
            ShowDeviceShadowResponse  response=client.showDeviceShadow(request);

            LinkedHashMap<String,Object> linkedHashMap = (LinkedHashMap<String, Object>) response.getShadow().get(0).getReported().getProperties();
            temperature = linkedHashMap.get("Temperature").toString();
            humidity = linkedHashMap.get("Humidity").toString();
            luminance = linkedHashMap.get("Luminance").toString();
            lightStatus = linkedHashMap.get("LightStatus").toString();
            motorStatus = linkedHashMap.get("MotorStatus").toString();
            settingTemperature=linkedHashMap.get("Setting_Tem").toString();
            settingHumidity=linkedHashMap.get("Setting_Hum").toString();
            settingLuminance=linkedHashMap.get("Setting_Lux").toString();
            ifLightOn=linkedHashMap.get("IfLightOn").toString();
            ifMotorOn=linkedHashMap.get("IfMotorOn").toString();
            System.out.println(temperature);

        } catch (ConnectionException | RequestTimeoutException e) {
            HiLog.error(LABEL,e.getMessage());
        } catch (ServiceResponseException e) {
            HiLog.error(LABEL,e.getErrorMsg());
        }
    }


}
